#include "mechanicalmotorspectrum.h"
#include "private/mechanicalspectrumprivatedefine.h"
#include "common/dataspecificationutils.h"
#include "common/binaryprocesshelper.h"
#include "common/xmldocument.h"
#include "private/spectrumdatafilecommondefine.h"
#include "common/xmlprocesshelper.h"
#include <QDebug>

namespace DataSpecificationGW
{
    class MechanicalMotorSpectrumPrivate
    {
    public:
        MechanicalMotorExtInformationPrivate m_stMechanicalMotorExtInformationPrivate;
    };

    MechanicalMotorSpectrum::MechanicalMotorSpectrum()
        : MechanicalSpectrum(),
          m_pMechanicalMotorSpectrumPrivate(new MechanicalMotorSpectrumPrivate)
    {
        setSpectrumTypeCode(SPECTRUM_CODE_MECH_MOTOR_I);
    }

    MechanicalMotorSpectrum::~MechanicalMotorSpectrum()
    {
    }

    /************************************************
     * 函数名   : spectrumName
     * 输入参数 : NULL
     * 输出参数 : NULL
     * 返回值   : QString
     * 功能     : 图谱名称
     ************************************************/
    QString MechanicalMotorSpectrum::spectrumName() const
    {
        return STR_FILE_NODE_MECHANICAL_MOTOR;
    }

    /************************************************
     * 函数名   : setMechanicalMotorExtInformation
     * 输入参数 :
       const MechanicalMotorExtInformation& stMechanicalMotorExtInformation: MechanicalMotorExtInformation
     * 输出参数 : NULL
     * 返回值   : void
     * 功能     : 设置MechanicalMotorExtInformation
     ************************************************/
    void MechanicalMotorSpectrum::setMechanicalMotorExtInformation(const MechanicalMotorExtInformation& stMechanicalMotorExtInformation)
    {
        m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType = stMechanicalMotorExtInformation.eMechDataType;
        m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType = stMechanicalMotorExtInformation.eOperatingMechanismType;
        m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType = stMechanicalMotorExtInformation.eMechStoredEnergyType;
        m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent = stMechanicalMotorExtInformation.fMotorStartCurrent;
        m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent = stMechanicalMotorExtInformation.fMotorMaxCurrent;
        m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime = stMechanicalMotorExtInformation.fMotorStorageTime;
        m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor = stMechanicalMotorExtInformation.fConversionFactor;
    }

    /************************************************
     * 函数名   : getMechanicalMotorExtInformation
     * 输入参数 : NULL
     * 输出参数 :
       MechanicalMotorExtInformation& stMechanicalMotorExtInformation: MechanicalMotorExtInformation
     * 返回值   : void
     * 功能     : 获取MechanicalMotorExtInformation
     ************************************************/
    void MechanicalMotorSpectrum::getMechanicalMotorExtInformation(MechanicalMotorExtInformation& stMechanicalMotorExtInformation)
    {
        stMechanicalMotorExtInformation.eMechDataType = static_cast<MechDataType>(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType);
        stMechanicalMotorExtInformation.eOperatingMechanismType = static_cast<OperatingMechanismType>(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType);
        stMechanicalMotorExtInformation.eMechStoredEnergyType = static_cast<MechStoredEnergyType>(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType);
        stMechanicalMotorExtInformation.fMotorStartCurrent = m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent;
        stMechanicalMotorExtInformation.fMotorMaxCurrent = m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent;
        stMechanicalMotorExtInformation.fMotorStorageTime = m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime;
        stMechanicalMotorExtInformation.fConversionFactor = m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor;
    }

    /************************************************
     * 函数名   : saveBinarySpectrumExtInfo
     * 输入参数 :
       QDataStream& out: 输出流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存二进制可扩展信息段
     ************************************************/
    bool MechanicalMotorSpectrum::saveBinarySpectrumExtInfo(QDataStream& out)
    {
        MechanicalSpectrum::saveBinarySpectrumExtInfo(out);

        out.device()->seek(MECH_COMMON_LENGTH);

        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechDataType) out << m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType;
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipOperatingMechanismType) out << m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType;
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechStoredEnergyType) out << m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType;
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStartCurrent) out << m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent;
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorMaxCurrent) out << m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent;
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStorageTime) out << m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime;
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipConversionFactor) out << m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor;

        return true;
    }

    /************************************************
     * 函数名   : saveXMLSpectrumExtInfo
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
       QDomElement& element: dom元素
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存XML可扩展信息段
     ************************************************/
    bool MechanicalMotorSpectrum::saveXMLSpectrumExtInfo(XMLDocument& xmlDocumentObj, QDomElement& element)
    {
        MechanicalSpectrum::saveXMLSpectrumExtInfo(xmlDocumentObj, element);

        xmlDocumentObj.beginElement(element);
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_EXTINFORMATION);

        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechDataType) xmlDocumentObj.setValue(TEXT_MECH_DATA_TYPE, QString::number(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType));
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipOperatingMechanismType) xmlDocumentObj.setValue(TEXT_MECH_FUNCTION_TYPE, QString::number(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType));
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechStoredEnergyType) xmlDocumentObj.setValue(TEXT_MECH_STORAGE_TYPE, QString::number(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType));
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStartCurrent) xmlDocumentObj.setValue(TEXT_MOTOR_START_CURRENT, QString::number(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent, 'f', 1));
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorMaxCurrent) xmlDocumentObj.setValue(TEXT_MOTOR_MAX_CURRENT, QString::number(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent, 'f', 1));
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStorageTime) xmlDocumentObj.setValue(TEXT_MOTOR_STORAGE_TIME, QString::number(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime, 'f', 1));
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipConversionFactor) xmlDocumentObj.setValue(TEXT_CONVERSION_FACTOR, QString::number(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor, 'f', 4));

        return true;
    }

    /************************************************
     * 函数名   : saveJSONSpectrumExtInfo
     * 输入参数 :
       rapidjson::Document::AllocatorType& alloc:
       rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存JSON可扩展信息段
     ************************************************/
    bool MechanicalMotorSpectrum::saveJSONSpectrumExtInfo(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& jsonValue)
    {
        MechanicalSpectrum::saveJSONSpectrumExtInfo(alloc, jsonValue);

        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechDataType) saveJSONField(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType, TEXT_MECH_DATA_TYPE, alloc, jsonValue);
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipOperatingMechanismType) saveJSONField(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType, TEXT_MECH_FUNCTION_TYPE, alloc, jsonValue);
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechStoredEnergyType) saveJSONField(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType, TEXT_MECH_STORAGE_TYPE, alloc, jsonValue);
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStartCurrent) saveJSONField(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent, TEXT_MOTOR_START_CURRENT, alloc, jsonValue);
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorMaxCurrent) saveJSONField(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent, TEXT_MOTOR_MAX_CURRENT, alloc, jsonValue);
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStorageTime) saveJSONField(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime, TEXT_MOTOR_STORAGE_TIME, alloc, jsonValue);
        if (!m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipConversionFactor) saveJSONField(m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor, TEXT_CONVERSION_FACTOR, alloc, jsonValue);

        return true;
    }

    /************************************************
     * 函数名   : parseBinarySpectrumExtInfo
     * 输入参数 :
       QDataStream& in: 输入流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析二进制可扩展信息段
     ************************************************/
    bool MechanicalMotorSpectrum::parseBinarySpectrumExtInfo(QDataStream& in)
    {
        MechanicalSpectrum::parseBinarySpectrumExtInfo(in);

        if (!in.device()->seek(MECH_COMMON_LENGTH))
        {
            qDebug() << "MechanicalMotorSpectrum::parseBinarySpectrumExtInfo failed";
            return false;
        }

        parseBinaryField(in, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechDataType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasmechDataType);
        parseBinaryField(in, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipOperatingMechanismType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasOperatingMechanismType);
        parseBinaryField(in, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMechStoredEnergyType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMechStoredEnergyType);
        parseBinaryField(in, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStartCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorStartCurrent);
        parseBinaryField(in, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorMaxCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorMaxCurrent);
        parseBinaryField(in, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipMotorStorageTime, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorStorageTime);
        parseBinaryField(in, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bSkipConversionFactor, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasConversionFactor);

        return in.status() == QDataStream::Ok;
    }

    /************************************************
     * 函数名   : parseXMLSpectrumExtInfo
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析XML可扩展信息段
     ************************************************/
    bool MechanicalMotorSpectrum::parseXMLSpectrumExtInfo(XMLDocument& xmlDocumentObj)
    {
        MechanicalSpectrum::parseXMLSpectrumExtInfo(xmlDocumentObj);

        xmlDocumentObj.beginElement(spectrumName());
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_EXTINFORMATION);       

        parseXMLField(xmlDocumentObj, TEXT_MECH_DATA_TYPE, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasmechDataType);
        parseXMLField(xmlDocumentObj, TEXT_MECH_FUNCTION_TYPE, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasOperatingMechanismType);
        parseXMLField(xmlDocumentObj, TEXT_MECH_STORAGE_TYPE, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMechStoredEnergyType);
        parseXMLField(xmlDocumentObj, TEXT_MOTOR_START_CURRENT, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorStartCurrent);
        parseXMLField(xmlDocumentObj, TEXT_MOTOR_MAX_CURRENT, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorMaxCurrent);
        parseXMLField(xmlDocumentObj, TEXT_MOTOR_STORAGE_TIME, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorStorageTime);
        parseXMLField(xmlDocumentObj, TEXT_CONVERSION_FACTOR, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasConversionFactor);

        xmlDocumentObj.endElement();
        xmlDocumentObj.endElement();

        return true;
    }

    /************************************************
     * 函数名   : parseJSONSpectrumExtInfo
     * 输入参数 :
       const rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析JSON可扩展信息段
     ************************************************/
    bool MechanicalMotorSpectrum::parseJSONSpectrumExtInfo(const rapidjson::Value& jsonValue)
    {
        MechanicalSpectrum::parseJSONSpectrumExtInfo(jsonValue);

        parseJSONField(jsonValue, TEXT_MECH_DATA_TYPE, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechDataType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasmechDataType);
        parseJSONField(jsonValue, TEXT_MECH_FUNCTION_TYPE, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucOperatingMechanismType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasOperatingMechanismType);
        parseJSONField(jsonValue, TEXT_MECH_STORAGE_TYPE, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.ucMechStoredEnergyType, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMechStoredEnergyType);
        parseJSONField(jsonValue, TEXT_MOTOR_START_CURRENT, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStartCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorStartCurrent);
        parseJSONField(jsonValue, TEXT_MOTOR_MAX_CURRENT, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorMaxCurrent, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorMaxCurrent);
        parseJSONField(jsonValue, TEXT_MOTOR_STORAGE_TIME, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fMotorStorageTime, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasMotorStorageTime);
        parseJSONField(jsonValue, TEXT_CONVERSION_FACTOR, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.fConversionFactor, m_pMechanicalMotorSpectrumPrivate->m_stMechanicalMotorExtInformationPrivate.bHasConversionFactor);

        return true;
    }

}
